package com.hang.util.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hang.mapper.TeacherMapper;
import com.hang.model.Teacher;
import com.hang.vo.TeacherVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

@Slf4j
public class TeacherListener extends AnalysisEventListener<TeacherVo> {
    private TeacherMapper teacherMapper;
    public TeacherListener(TeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }

    //一行一行读取
    @Override
    public void invoke(TeacherVo teacherVo, AnalysisContext analysisContext) {
        if(!ObjectUtils.isEmpty(teacherVo.getTeacherId())){
            //调用方法添加数据库
            Teacher teacher = new Teacher();
            //简单算法：提取身份证后六位作为默认密码
            teacher.setPassword(teacherVo.getIdentity().substring(teacherVo.getIdentity().length()-6));

            BeanUtils.copyProperties(teacherVo,teacher);
            teacherMapper.insert(teacher);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("教师信息导入完成");
    }
}